#include <stdio.h>
#include <curses.h>

extern short Sys_Request;

unsigned char Kbd_Matrix[65];

// ascii code (index) to ORIC code (value)
// ORIC code = 3 bits giving position of the negative bit on PortA
// + 3 bits of PB0..2 + 1 bit for shift press (1 yes, 0 no)
// see also http://oric.free.fr/programming.html (Keyboard section)
static signed char keycode_to_oric[] =//  0   1   2   3   4   5   6   7
{ -1, -1, -1, -1, -1, -1, -1, -1, //  0: ??? ??? ??? ??? ??? ??? ??? ???
  45, 34, 47, -1, -1, 47, -1, -1, //  8: DEL CTL RET ??? ??? RET ??? ???
  -1, -1, -1, -1, -1, -1, -1, -1, // 16: ??? ??? ??? ??? ??? ??? ??? ???
  -1, -1, -1, 41, -1, -1, -1, -1, // 24: ??? ??? ??? ESC ??? ??? ??? ???
   8, 81,119,113, 53, 33,  1,118, // 32: SPC !/1 "/' #/3 $/4 %/5 &/7  "
  23, 47, 15,127, 24, 54, 40, 62, // 40: (/9 )/0 *-8 +/=  ,   -   .   /
  46, 80,100,112, 52, 32, 20,  0, // 48:  0   1   2   3   4   5   6   7
  14, 22, 39, 38, 25,126, 41, 63, // 56:  8   9  :/;  ;  </,  =  >/. ?//
 101, 92, 36,116,114, 60, 50, 44, // 64: @/2  A   B   C   D   E   F   G
  28, 26,  2,  6, 30,  4, 16, 42, // 72:  H   I   J   K   L   M   N   O
  58, 98, 34,108, 18, 10, 48,124, // 80:  P   Q   R   S   T   U   V   W
  96, 12, 84,122,102,106, 21, 55, // 88:  X   Y   Z   [   \   ]  ^/6 PND
  -1, 92, 36,116,114, 60, 50, 44, // 96: ???  a   b   c   d   e   f   g
  28, 26,  2,  6, 30,  4, 16, 42, //104:  h   i   j   k   l   m   n   o
  58, 98, 34,108, 18, 10, 48,124, //112:  p   q   r   s   t   u   v   w
  96, 12, 84,123,103,107, -1, 90, //120:  x   y   z  {/[ |\\ }\] ??? DEL
  52, 28, 44, 60                  //128: UP  DWN LFT RGT       <-- special keys
};

int lastkey = 0;
int waitup = 0;
int kticker = 0;

void poll_keyboard() {
    int ch, i;
    unsigned char code;
    kticker++;
    if(kticker < 20) return;
    if(waitup) {
        code = keycode_to_oric[lastkey];
        if(lastkey > 31 && lastkey < 128) {
            if(code & 1)Kbd_Matrix[36 & 7] &= ~ 1 << (36 >> 3);
            code = code >> 1;
        }
        Kbd_Matrix[code & 7] &= ~ 1 << (code >> 3);
        waitup = 0;
        return;
    }
    ch = getch();
    if(ch == ERR) return;
    //mvprintw(30, 0, "   ");
    //mvprintw(30, 0, "%0d",ch);
    if(ch > 127) {
        switch(ch) {
            case KEY_BACKSPACE: ch = 8; break;
            case KEY_DOWN:      ch = 128; break;
            case KEY_UP:        ch = 129; break;
            case KEY_LEFT:      ch = 130; break;
            case KEY_RIGHT:     ch = 131; break;
            case KEY_F(7):      Sys_Request |= 0x400; return; // NMI
            case KEY_F(8):      Sys_Request |= 0x800; return; // RESET
            default: ch = 0;
        }
    }
    if(keycode_to_oric[ch] == -1) return;
    if(ch == KEY_BACKSPACE) ch = 8;
    code = keycode_to_oric[ch];
    if(ch > 31 && ch < 128) {
        if(code & 1) Kbd_Matrix[36 & 7] |=  1 << (36 >> 3);
        code = code >> 1;
    }
    Kbd_Matrix[code & 7] |=  1 << (code >> 3);
    kticker = 0;
    waitup = 1;
    lastkey = ch;
    refresh();
}

void Read_Joystick_on_printer_port (void) {
    //TODO
}


void Read_Joystick_on_VIA2 (void) {
  /* FIXME */
}
